package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.brand.TbBrand;
import com.dongdongshop.pojo.specification.TbSpecification;
import com.dongdongshop.pojo.typeTemplate.TbTypeTemplate;
import com.dongdongshop.service.Brand.BrandService;
import com.dongdongshop.service.specification.SpecificationService;
import com.dongdongshop.service.typeTemplate.TypeTemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("typeTemplate")
public class TypeTemplateController {

    @Reference
    private TypeTemplateService typeTemplateService;

    @Reference
    private BrandService brandService;

    @Reference
    private SpecificationService specificationService;

    private Logger logger = LoggerFactory.getLogger(TypeTemplateController.class);

    /*
    * 跳转模板管理页
    * */
    @RequestMapping("toTypeTemplateList")
    public String toTypeTemplateList(Model model){
        try {
            //查询所有的品牌信息
            List<TbBrand> brands = brandService.findBrandsAll();
            //查询所有的规格信息
            List<TbSpecification> specifications = specificationService.getSpecificationInfo();
            model.addAttribute("brands",brands);
            model.addAttribute("specifications",specifications);
        }catch (Exception e){
            logger.error("跳转模板页面方法异常",e);
            return "typeTemplate/type_template";
        }
        return "typeTemplate/type_template";
    }
    /*
    * 查询所有模板信息
    * */
    @RequestMapping(value = "findTypeTemplateAll",method = RequestMethod.POST)
    @ResponseBody
    public DataResult findTypeTemplateAll(){
        List<TbTypeTemplate> tts = null;
        try {
            //查询所有模板信息
            tts = typeTemplateService.findTypeTemplateAll();
        }catch (Exception e){
            logger.error("查询所有模板信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),tts);
    }
    /*
    * 增加分类模板信息
    * */
    @RequestMapping(value = "addTypeTemplate",method = RequestMethod.POST)
    @ResponseBody
    public DataResult addTypeTemplate(TbTypeTemplate tt){
        try {
            typeTemplateService.addTypeTemplate(tt);
        }catch (Exception e){
            logger.error("增加分类模板异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
    /**
     * 根据分类模板id进行查询
     */
    @RequestMapping(value = "getTypeTemplateById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult getTypeTemplateById(Long id){
        TbTypeTemplate tt = null;
        try {
            tt = typeTemplateService.getTypeTemplateById(id);
        }catch (Exception e){
            logger.error("根据分类模板id进行查询异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),tt);
    }
    /*
    * 根据id修改分类模板信息
    * */
    @RequestMapping(value = "updateTypeTemplateById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult updateTypeTemplateById(TbTypeTemplate tt){
        try {
            typeTemplateService.updateTypeTemplateById(tt);
        }catch (Exception e){
            logger.error("根据分类模板id进行查询异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
    /*
    * 根据商品类型模板的id进行批量删除
    * */
    @RequestMapping(value = "deleteTypeTemplateByIds",method = RequestMethod.POST)
    @ResponseBody
    public DataResult deleteTypeTemplateByIds(@RequestParam("ids[]")Integer[] ids){
        try {
            typeTemplateService.deleteTypeTemplateByIds(ids);
        }catch (Exception e){
            logger.error("根据商品类型模板的id进行批量删除异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
}
